import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)


const routes = [

    {
        path: '/',
        name: 'blog',
        component: resolve => require(['../views/Head'], resolve),
        redirect: '/sHome',
        children: [
            {path: 'sHome', name: 'SHome', component: resolve => require(['../views/Home'], resolve)},
            {
                path: 'sBlog',
                name: 'SBlog',
                component: resolve => require(['../views/Blog'], resolve),
                children: [
                    {
                        path: 'comment',
                        name: 'Comment',
                        component: resolve => require(['../views/comment/Comment'], resolve),
                        children:[
                            {
                                path: 'reply',
                                name: 'Reply',
                                component: resolve => require(['../views/comment/Reply'], resolve),
                            }
                        ],
                    }
                ]
            },
            {path: 'sType', name: 'SType', component: resolve => require(['../views/Type'], resolve)},
            {path: 'sTag', name: 'STag', component: resolve => require(['../views/Tag'], resolve)},
            {path: 'sImage', name: 'SImage', component: resolve => require(['../views/Image'], resolve)},
            {path: 'sAbout', name: 'SAbout', component: resolve => require(['../views/About'], resolve)},

        ],
    },

    {
        path: '/admin',
        name: 'Head',
        component: resolve => require(['../views/admin/Head'], resolve),
        redirect:
            '/admin/home',
        children:
            [
                {path: 'home', name: 'Home', component: resolve => require(['../views/admin/Home'], resolve)},
                {path: 'blogs', name: 'Blogs', component: resolve => require(['../views/admin/Blogs'], resolve)},
                {path: 'blog', name: 'Blog', component: resolve => require(['../views/admin/Blog'], resolve)},
                {path: 'type', name: 'Type', component: resolve => require(['../views/admin/Type'], resolve)},
                {path: 'tag', name: 'Tag', component: resolve => require(['../views/admin/Tag'], resolve)},
                {path: 'image', name: 'Image', component: resolve => require(['../views/admin/Image'], resolve)},
                {path: 'link', name: 'Link', component: resolve => require(['../views/admin/Link'], resolve)},
                {
                    path: 'userNews',
                    name: 'UserNews',
                    component: resolve => require(['../views/admin/user/UserNews'], resolve)
                },
            ]
    },


]

const router = new VueRouter({
    routes
})
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
    //将所有需要登陆才显示的页面的路由都放进一个数组
    const nextRoute = ['Head', 'Home', 'Blogs', 'Blog', 'Type','Tag','Image','Link','UserNews'];
    // to代表将要访问的路径
    //from代表从哪个路径跳转而来
    // next是一个函数，表示放行， next（） 放行     强制跳转
    if (to.name == 'SHome') return next()
    if (to.name == 'SBlog') return next()
    if (to.name == 'SType') return next()
    if (to.name == 'STag') return next()
    if (to.name == 'SImage') return next()
    if (to.name == 'SAbout') return next()
    //获取token的user对象
    //如果token为空跳转到首页
    if(nextRoute.indexOf(to.name) >= 0) {  //判断该页面是否需要登陆
        if(window.sessionStorage.getItem("user") == null) {   //判断登陆状态
            next({ name : 'SHome'})   //如果未登录，则跳转到登录页
        } else {
            next()  //如果已经登陆，那就可以跳转
        }
    }


})

export default router


//
// import Vue from 'vue'
// import VueRouter from 'vue-router'
//
// // 前台路由
// import SHome from '../views/Home.vue'
// import SHead from '../views/Head.vue'
// import SBlog from '../views/Blog.vue'
// import SType from '../views/Type.vue'
// import STag from '../views/Tag.vue'
// import SImage from '../views/Image.vue'
// import SAbout from '../views/About.vue'
// import Comment from '../views/comment/Comment.vue'
// import Reply from '../views/comment/Reply.vue'
//
//
// // 后台路由
// import Home from '../views/admin/Home.vue'
// import Head from '../views/admin/Head.vue'
// import Blogs from '../views/admin/Blogs.vue'
// import Blog from '../views/admin/Blog.vue'
// import Type from '../views/admin/Type.vue'
// import Tag from '../views/admin/Tag.vue'
// import Image from '../views/admin/Image.vue'
// import Link from '../views/admin/Link.vue'
// import UserNews from '../views/admin/user/UserNews.vue'
//
// Vue.use(VueRouter)
//
// const routes = [
//
//     {
//         path: '/',
//         name: 'blog',
//         component: SHead,
//         redirect:'/sHome',
//         children: [
//             {path: 'sHome', name: 'SHome', component: SHome},
//             {
//                 path: 'sBlog',
//                 name: 'SBlog',
//                 component: SBlog,
//                 children:[
//                     {path:'comment',
//                         name:'Comment',
//                         component:Comment,
//                         children:[
//                             {path:'reply',
//                             name:'Reply',
//                             component: Reply,
//                             }
//                         ],
//                     }
//                 ],
//             },
//             {path: 'sType', name: 'SType', component: SType},
//             {path: 'sTag', name: 'STag', component: STag},
//             {path: 'sImage', name: 'SImage', component: SImage},
//             {path: 'sAbout', name: 'SAbout', component: SAbout},
//
//         ],
//     },
//
//     {
//         path: '/admin',
//         name: 'Head',
//         component: Head,
//         redirect:'/admin/home',
//         children: [
//             {path: 'home', name: 'Home', component: Home},
//             {path: 'blogs', name: 'Blogs', component: Blogs},
//             {path: 'blog', name: 'Blog', component: Blog},
//             {path: 'type', name: 'Type', component: Type},
//             {path: 'tag', name: 'Tag', component: Tag},
//             {path: 'image', name: 'Image', component: Image},
//             {path: 'link', name: 'Link', component: Link},
//             {path: 'userNews', name: 'UserNews', component: UserNews},
//         ]
//     },
//
//
// ]
//
// const router = new VueRouter({
//     routes
// })
//
// //挂载路由导航守卫
// router.beforeEach((to, from, next) => {
//     //将所有需要登陆才显示的页面的路由都放进一个数组
//     const nextRoute = ['Head', 'Home', 'Blogs', 'Blog', 'Type','Tag','Image','Link','UserNews'];
//     // to代表将要访问的路径
//     //from代表从哪个路径跳转而来
//     // next是一个函数，表示放行， next（） 放行     强制跳转
//     if (to.name == 'SHome') return next()
//     if (to.name == 'SBlog') return next()
//     if (to.name == 'SType') return next()
//     if (to.name == 'STag') return next()
//     if (to.name == 'SImage') return next()
//     if (to.name == 'SAbout') return next()
//     //获取token的user对象
//     //如果token为空跳转到首页
//     if(nextRoute.indexOf(to.name) >= 0) {  //判断该页面是否需要登陆
//         if(window.sessionStorage.getItem("user") == null) {   //判断登陆状态
//             next({ name : 'SHome'})   //如果未登录，则跳转到登录页
//         } else {
//             next()  //如果已经登陆，那就可以跳转
//         }
//     }
//
//
// })
//
// export default router
